/*
 * GTFSTrips.java
 * 
 * Created by demory on Jul 6, 2009, 3:53:59 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.core.transit.gtfs;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.fpdev.core.FPEngine;
import org.fpdev.core.transit.Route;
import org.fpdev.core.transit.SubRoute;
import org.fpdev.core.transit.TransitPath;

/**
 *
 * @author demory
 */
public class GTFSTrips extends GTFSDBTable {

  //private Map<String, Set<String>> subsTable_;

  public GTFSTrips(File file, GTFSImporter importer) {
    super("trips", file, importer);
    //subsTable_ = new HashMap<String, Set<String>>();
  }

  /*public void initTrips() {
    int rteCol = columnNames_.get("route_id");
    int dirCol = columnNames_.get("direction_id");
    int svcCol = columnNames_.get("service_id");
    int shpCol = columnNames_.get("shape_id");

    for(String[] row : rows_) {
      String rteID = row[rteCol];
      if(!subsTable_.containsKey(rteID)) subsTable_.put(rteID, new HashSet<String>());
      Set<String> subs = subsTable_.get(rteID);

      //System.out.println("shp: "+row[shpCol]);
      String subID = "D"+row[dirCol]+"_"+row[svcCol]+"_"+row[shpCol].split("\\.")[2];
      subs.add(subID);
    }

    for(Map.Entry<String, Set<String>> entry : subsTable_.entrySet()) {
      System.out.println("Route "+entry.getKey());
      for(String subID : entry.getValue())
        System.out.println(" - "+subID);
    }
  }*/

  public void initSubs(Route rte, FPEngine engine) {
    List<String[]> rows = db_.getData(name_, "route_id = \""+rte.getID()+"\"");
    int shpCol = columnNames_.get("shape_id");
    Set<String> shapeIDs = new HashSet<String>();
    for(String[] row : rows) {
      shapeIDs.add(row[shpCol]);
    }

    TransitPath.Type type = importer_.getRoutes().getRouteType(rte);
    if(type == null) {
      System.out.println("Could not initialize "+rte.getID()+", invalid type");
      return;
    }

    for(String shapeID : shapeIDs) {
      System.out.println("shape: "+shapeID);
      SubRoute sub = new SubRoute(rte, shapeID, engine, type);
      rte.addSubRoute(sub);
    }
  }
}
